package org.soulraven.games.memory.ui.game.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;

import org.soulraven.games.memory.engine.controller.Controller;
import org.soulraven.games.memory.ui.game.GameFrame;
import org.soulraven.games.memory.util.MessageUtil;

public class GameController extends Controller {

	public static final String EXIT_MENU_ITEM = "EXIT_MENU_ITEM";
	public static final String ABOUT_MENU_ITEM = "ABOUT_MENU_ITEM";

	private GameFrame frame;

	public GameController(final GameFrame frame) {
		super();
		this.frame = frame;
		getResolvers().put(EXIT_MENU_ITEM, new ExitActionResolver());
		getResolvers().put(ABOUT_MENU_ITEM, new AboutActionResolver());
	}

	@Override
	public void windowClosing(WindowEvent e) {
		onExit();
	}

	@Override
	public void onExit() {
		String message = MessageUtil.getMessage("confirmonexit");
		String title = MessageUtil.getMessage("exit");
		int dialogOption = JOptionPane.showConfirmDialog(frame, message, title,
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if (JOptionPane.YES_OPTION == dialogOption) {
			System.exit(0);
		}
	}

	private class AboutActionResolver implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
		}
	}

	private class ExitActionResolver implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			onExit();
		}
	}
}
